<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\UpdateUserRequest;
use App\Http\Resources\UserResource;
use App\Models\Image;
use App\Models\User;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    //

    public function show(User $user)
    {
        return new UserResource($user);
    }


    public function me(Request $request)
    {
        return (new UserResource($request->user()))->showProtectedFields();
    }


    public function update(UpdateUserRequest $request)
    {
        $user = $request->user();

        $user->name = $request->name;
        $user->introduction = $request->introduction;

        if ($avaImaId = $request->avatar_image_id) {
            $image = Image::find($avaImaId);
            if (isset($image)) $user->avatar = $image->path;
        }

        $user->update();

        return (new UserResource($user))->showProtectedFields();
    }
}
